package lxs.swift.operation.match;

import org.jetbrains.annotations.NotNull;

import java.util.Objects;
import java.util.function.Supplier;


public class Match<T, C extends Case<T>> {
    private final T value;

    private Match(T value) {
        this.value = value;
    }

    @SafeVarargs
    public final <R, TD extends Todo<T, R>> R of(@NotNull Item<T, R, C, TD>... items) {
        Objects.requireNonNull(items);
        for (Item<T, R, C, TD> item : items) {
            if (item.tCase.test(value)) {
                return item.tTodo.todo(value);
            }
        }
        throw new RuntimeException("cannot be matched exhaustively, so a wildcard `$(other)` is necessary");
    }


    public static <T, C extends Case<T>> Match<T, C> from(T value) {
        return new Match<>(value);
    }

    public static <T, C extends Case<T>> Match<T, C> from(@NotNull Supplier<T> supplier) {
        Objects.requireNonNull(supplier);
        return new Match<>(supplier.get());
    }


}
